import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtCourseLearnRecordApi } from '#/api/cmt/course/learnRecord';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getCourseInfoOptions } from '#/api/cmt/course/info';
import { getCourseChapterOptions } from '#/api/cmt/course/chapter';

export const IS_COMPLETED_OPTIONS = [{ label: '是', value: 1 }, { label: '否', value: 0 }];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'courseId',
      label: '课程ID',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择课程',
        api: getCourseInfoOptions,
        resultField: 'list',
        labelField: 'title',
        valueField: 'id',
      },
    },
    {
      fieldName: 'chapterId',
      label: '章节ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择章节',
        api: getCourseChapterOptions,
        resultField: 'list',
        labelField: 'title',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      rules: 'required',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full',
      },
    },
    {
      fieldName: 'progress',
      label: '学习进度(秒)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full', min: 0,
      },
    },
    {
      fieldName: 'progressPercent',
      label: '进度百分比',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full', min: 0, max: 100,
      },
    },
    {
      fieldName: 'isCompleted',
      label: '是否完成',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: IS_COMPLETED_OPTIONS,
      },
    },
    {
      fieldName: 'completeTime',
      label: '完成时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择完成时间',
      },
    },
    {
      fieldName: 'lastLearnTime',
      label: '最后学习时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择最后学习时间',
      },
    },
    {
      fieldName: 'learnDuration',
      label: '学习时长(秒)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入{field_label}', controlsPosition: 'right', class: '!w-full', min: 0,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtCourseLearnRecordApi.CourseLearnRecord>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'courseId',
      title: '课程ID',
      minWidth: 120,
    },
    {
      field: 'chapterId',
      title: '章节ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '会员ID',
      minWidth: 120,
    },
    {
      field: 'progress',
      title: '学习进度',
      minWidth: 120,
    },
    {
      field: 'progressPercent',
      title: '进度百分比',
      minWidth: 120,
    },
    {
      field: 'isCompleted',
      title: '是否完成',
      minWidth: 120,
    },
    {
      field: 'learnDuration',
      title: '学习时长',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
